package com.example.leetdemo.finash;

public class Solution904 {


    public int totalFruit(int[] fruits) {
        int max = 2;
        int l = 0, r = -1, min = l;
        for (int i = 1; i < fruits.length; i++) {
            if (r != -1) {
                if (fruits[l] != fruits[i] && fruits[r] != fruits[i]) {
                    max = Math.max(i - min, max);
                    l = r; r = i; min = l;
                } else {
                    if (fruits[l] == fruits[i]) {
                        l = r;
                        r = i;
                    }
                }
            } else {
                if (fruits[i] != fruits[l]) {
                    max = Math.max(max, i - l + 1);
                    r = i;
                }
            }
        }
        max = Math.max(fruits.length - min, max);
        return max;
    }

    public static void main(String[] args) {
        Solution904 solution = new Solution904();
//        solution.totalFruit(new int[] {3,3,3,1,2,1,1,2,3,3,4});
//        solution.totalFruit(new int[] {0,1,2,2});
        solution.totalFruit(new int[] {1,2,1});
    }

}
